package top.hkyzf.study.leetcode.tree;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 *
 * @author zhufeng
 * @version 1.0
 * @date 2024-2-18 23:17
 */
public class N叉树的前序遍历_589 {

    public List<Integer> preorder(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        helper(root, result);
        return result;
    }

    public void helper(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        if (root.children == null) {
            return;
        }
        for (TreeNode node : root.children) {
            helper(node, result);
        }
    }

    @Test
    public void testNode () {
        TreeNode root = new TreeNode(1);
        TreeNode c1 = new TreeNode(3);
        TreeNode c2 = new TreeNode(2);
        TreeNode c3 = new TreeNode(4);
        List<TreeNode> ch1 = new ArrayList<>();
        ch1.add(c1);
        ch1.add(c2);
        ch1.add(c3);
        root.children = ch1;
        TreeNode cc1 = new TreeNode(5);
        TreeNode cc2 = new TreeNode(6);
        List<TreeNode> cch1 = new ArrayList<>();
        cch1.add(cc1);
        cch1.add(cc2);
        c1.children = cch1;
        System.out.println(preorder(root));
    }
}
